﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Indie_Game_Contest
{
    class AIState
    {
        //////////////////////////////////////////////////////////////////////////
        // Data Members
        Enemy enemy;
        float atkTimer = 0.0f;

        //////////////////////////////////////////////////////////////////////////
        // Methods
        public void Update(float dt, Enemy org)
        {
            Vector2 norm = GameplayState.Instance.Player.Position - org.Position;

            // Ready to attack?
            if (atkTimer > .5f)
            {
                atkTimer = 0.0f;

                // Close enough to attack?
                if (norm.Length() < (GameplayState.Instance.Player.Center.X * GameplayState.Instance.Player.Scale +
                                                enemy.Center.X * enemy.Scale) + 8)
                {
                    org.Attack1 = true;
                    GameplayState.Instance.Player.Take_Damage(10);
                    GameplayState.Instance.Player.Position += new Vector2(15 * (norm.X / Math.Abs(norm.X)), 0);
                }
            }
            

            // Move towards the player if we're not already to close
            //float d = Math.Abs(norm.X/norm.Y);
            if (Math.Abs(norm.X) > (enemy.Center.X*enemy.Scale + GameplayState.Instance.Player.Center.X) + 24)
            {
                float normX = norm.X / Math.Abs(norm.X);
                norm.Normalize();

                if (norm.Y > -.5f && norm.Y < .9f)
                {
                    org.Velocity += new Vector2(normX * 50, 0);

                    if (Math.Abs(enemy.Velocity.X) > 100)
                        enemy.Velocity = new Vector2(enemy.Velocity.X/Math.Abs(enemy.Velocity.X)*100,enemy.Velocity.Y);
                }               
            }

            atkTimer += dt;
        }

        //////////////////////////////////////////////////////////////////////////
        // Properties
        public Indie_Game_Contest.Enemy Enemy
        {
            get { return enemy; }
            set { enemy = value; }
        }
    }
}
